package com.china08.yxyapi.api.home.activity.healthcompetition.tongjipage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionSchool;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

/**
 * 健康知识竞赛 分学校统计参与学生总数
 * 
 * @author yyy
 *
 */
@CrossOrigin
@RestController
public class HealthCompetionSchTotalTongjiController extends YxyApiBaseController {

	@Autowired
	private MongoTemplate mongoTemplate;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/huodongtongji/schtotal", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel process(//
			HttpServletRequest request, //
			HttpServletResponse response, // ,
			@RequestParam(value = "code") String code) {
		List<String> noList = new ArrayList<>();
		noList.add("ff8080815ba9422a015bb326acfa1599");
		noList.add("ff8080815ac67d12015b03a25cf70a5c");
		noList.add("ff8080815a032de4015a3c0fdf2d7a16");
		noList.add("ff808081598c885c01599b2857f52de7");
		noList.add("ff80808148f98a490148fcdab1760008");
		RespModel respModel = new RespModel();
		// 分学校统计参与学生总数
		TypedAggregation<YxyCompetitionSchool> count = Aggregation.newAggregation(YxyCompetitionSchool.class,
				Aggregation.match(Criteria.where("areaCodeDistrict").is(code)),
				Aggregation.group("schoolId").first("schoolId").as("code").first("schoolName").as("name")
						.first("joinTotal").as("total").first("rate").as("rate").first("avg").as("avg"),
				Aggregation.sort(Direction.DESC, "total", "rate", "avg"));
		AggregationResults<SchoolTotal> resultZong = mongoTemplate.aggregate(count, SchoolTotal.class);
		List<SchoolTotal> dateModelLst = resultZong.getMappedResults();
		// 过滤测试学校
		for (Iterator<SchoolTotal> car = dateModelLst.iterator(); car.hasNext();) {
			SchoolTotal score = car.next();
			if (noList.contains(score.code)) {
				// list.remove(score);
				car.remove();
			}
		}
		if (dateModelLst.size() > 15) {
			dateModelLst = dateModelLst.subList(0, 15);
		}
		respModel.data = dateModelLst;
		return respModel;
	}

	protected static class ReqModel extends YxyApiReqModel {
	}

	protected static class RespModel extends YxyApiRespModel {
		// 分区县统计集合
		public List<SchoolTotal> data = new ArrayList<>();
	}

	protected static class SchoolTotal {
		// 学校名字
		public String name;
		// 学校id
		public String code;
		// 各学校总参与竞赛人数
		public Integer total = 0;
		// 各学校总参与率
		public Double rate;
		// 各学校平均分
		public Double avg;
	}

}
